using System;
using System.Collections.Generic;
using System.Text;
using Spacedash.Game.Cameras;
using Barrage;
using Barrage.Global;
using SlimDX;
using Spacedash.Game.Nodes;
using Spacedash.Game.Items;
using Spacedash.Logic.Scenario;
using Spacedash.Logic;
using Barrage.Items;
using Barrage.Textures;
using Barrage.Scenes;
using Barrage.Meshes.Text;
using Barrage.Items._2D;
using Spacedash.Logic.RaceStates;
using Barrage.Language;

namespace Spacedash.Game.Scenes
{
    /// <summary>
    /// 2D Scene which renders all the HUD elements during the race
    /// </summary>
    public class RaceHUDScene : Scene2D
    {
        const string FINISHED = "FINISHED";
        const string FINISH_POSITION = "FINISH_POSITION";
        const string RACE_STARTED = "RACE_STARTED";
        BasicTextBox infoText;
        Race race;
        Ship ship;
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="race">Race instance</param>
        /// <param name="ship">Ship owner of this hud scene</param>
        public RaceHUDScene(Race race,Ship ship)
        {
            this.race = race;
            this.ship = ship;

            addDefaultElements();
        }

         void addDefaultElements()
        {
            Vector2 pixelPos=GUIPosition(GUIBorders.BottomRight,new Vector2(0.05f,0.05f),new Vector2(GUISpeedometer.SIZE_X,GUISpeedometer.SIZE_Y));
            Add(new GUISpeedometer(pixelPos,  ship));
            Add(new GUILapCounter(GUIPosition(GUIBorders.TopRight,new Vector2(0.15f,0.075f), new Vector2(GUILapCounter.SIZE_X,GUILapCounter.SIZE_Y)),race, ship));
            //Add(new GUIShipPosition(GUIPosition(GUIBorders.TopRight, new Vector2(0.15f, 0.275f), new Vector2(GUIShipPosition.SIZE_X, GUIShipPosition.SIZE_Y)), race, ship));
            Add(new GUIShipOffsets(GUIPosition(GUIBorders.BottomRight, new Vector2(0.6f,0.475f), Vector2.Zero), race, ship));
            //Add(new GUILapTimes(GUIPosition(GUIBorders.TopLeft, new Vector2(0.325f, 0.075f), new Vector2(GUILapTimes.SIZE_X, GUILapTimes.SIZE_Y)), race, ship));
            Add(new GUIMinimap(GUIPosition(GUIBorders.BottomLeft, new Vector2(0.05f, 0.05f), new Vector2(GUIMinimap.SIZE_X, GUIMinimap.SIZE_Y)), race, ship));
            infoText = new BasicTextBox(BitmapFontManager.Fonts["Standard"],new TextBoxDrawProperties(TextAlignment.Center,new Vector2(Engine.DefaultRenderTargetDescription.Width,Engine.DefaultRenderTargetDescription.Height),1,VerticalAlignment.Center,Vector2.Zero));
            Add(infoText);
        }

        /// <summary>
        /// \internal Updates some message data and other items in the scene
        /// </summary>
        public override void Update()
        {
            switch(race.State.State)
            {
                case RaceStateList.Starting:
                    infoText.Visible = true;
                    infoText.Active = true;
                    infoText.Text = Math.Ceiling(((StartingRaceState)race.State).Remaining).ToString();
                    break;
                case RaceStateList.Finishing:
                    infoText.Visible=true;
                    infoText.Active = true;
                    infoText.Text = LanguageManager.Texts[FINISHED] + "\n" + String.Format(LanguageManager.Texts[FINISH_POSITION], ship.RacePosition+1);
                    break;
                case RaceStateList.Racing:
                    bool showMessage = ((RacingRaceState)race.State).ElapsedTime<=2000;
                    infoText.Visible = showMessage;
                    infoText.Active = showMessage;
                    infoText.Text = LanguageManager.Texts[RACE_STARTED];
                    if (ship.State == ShipState.Finished)
                    {
                        infoText.Visible = true;
                        infoText.Active = true;
                        infoText.Text = LanguageManager.Texts[FINISHED] + "\n" + String.Format(LanguageManager.Texts[FINISH_POSITION], ship.RacePosition+1);
                    }
                    break;
                default:
                    infoText.Visible = false;
                    infoText.Active = false;
                    break;
            }
            base.Update();
        }
        /// <summary>
        /// \internal Clear resources
        /// </summary>
        public override void Dispose()
        {
            infoText = null;
            base.Dispose();
        }
    }
}
